# Imports after code execution state reset
from pptx import Presentation
from pptx.util import Inches
from io import BytesIO
from PIL import Image, ImageDraw
import util,sys
# 模拟生成几组图片流（使用 PIL）
def generate_mock_image(label, size=(200, 200), color=(200, 200, 255)):
    img = Image.new("RGB", size, color)
    draw = ImageDraw.Draw(img)
    draw.text((10, 10), label, fill=(0, 0, 0))
    stream = BytesIO()
    img.save(stream, format="PNG")
    stream.seek(0)
    return stream

images_list = []
for i in range(4):
    group = {
        "TOP View": generate_mock_image(f"top_view_{i+1}"),
        "north": generate_mock_image(f"north_{i+1}", size=(160, 80), color=(255, 200, 200)),
        "south": generate_mock_image(f"south_{i+1}", size=(160, 80), color=(200, 255, 200)),
        "west": generate_mock_image(f"west_{i+1}", size=(80, 160), color=(255, 255, 200)),
        "east": generate_mock_image(f"east_{i+1}", size=(80, 160), color=(200, 255, 255)),
        "bottom": generate_mock_image(f"bottom_{i+1}", size=(80, 160), color=(200, 255, 255)),
        "acf_pad": generate_mock_image(f"acf_pad_{i+1}", size=(80, 160), color=(200, 255, 255)),
    }
    images_list.append(group)

# Function to create expanded layout PPT
prs_width = 13.3
prs_height = 7.5
def create_layout_ppt(prs,images_list,title):

    img_sizes = {
        "TOP View": (Inches(1.1), Inches(1.2)),
        "North": (Inches(1.1), Inches(0.4)),
        "South": (Inches(1.1), Inches(0.4)),
        "West": (Inches(0.3), Inches(1.2)),
        "East": (Inches(0.3), Inches(1.2)),
        "Bottom View": (Inches(1.1), Inches(1.2)),
        "ACF Pad": (Inches(1.1), Inches(0.4)),
        "NG Location": (Inches(1.1), Inches(1.2)),
    }

    max_per_slide = 3  # Max sets per slide

    def add_expanded_layout(slide, imgs, offset_x):

        center_left = offset_x + Inches(0.6)
        center_top = Inches(2)

        top_w, top_h = img_sizes["TOP View"]
        slide.shapes.add_picture(imgs["TOP View"], center_left, center_top, width=top_w, height=top_h)


        # Add surrounding images
        north_w, north_h = img_sizes["North"]
        north_left = center_left + (top_w - north_w) / 2
        north_top = center_top - north_h - Inches(0.1)
        slide.shapes.add_picture(imgs["North"], north_left,north_top,width=north_w, height=north_h)
        util.add_text_box(slide,north_left,north_top-Inches(0.3),Inches(0.2),Inches(0.2),"North",10)
        util.add_text_box(slide,north_left,north_top-Inches(0.5),Inches(0.2),Inches(0.2),"TOP View",10)


        south_w, south_h = img_sizes["South"]
        south_left = center_left + (top_w - south_w) / 2
        south_top = center_top + top_h + Inches(0.1)

        slide.shapes.add_picture(imgs["South"], south_left,south_top,width=south_w, height=south_h)
        util.add_text_box(slide,south_left,south_top+Inches(0.4),Inches(0.2),Inches(0.2),"South",10)


        west_w, west_h = img_sizes["West"]
        west_left = center_left - west_w - Inches(0.1)
        west_top = center_top + (top_h - west_h) / 2
        slide.shapes.add_picture(imgs["West"], west_left,west_top,width=west_w, height=west_h)
        util.add_text_box(slide,west_left-Inches(0.2),west_top+west_h/2,Inches(0.2),Inches(0.2),"West",10)

        east_w, east_h = img_sizes["East"]
        east_left = center_left + top_w + Inches(0.1)
        east_top = center_top + (top_h - east_h) / 2
        slide.shapes.add_picture(imgs["East"], east_left,east_top,width=east_w, height=east_h)
        util.add_text_box(slide,east_left,east_top+east_h/2,Inches(0.2),Inches(0.2),"East",10)

        bottom_w, bottom_h = img_sizes["Bottom View"]
        bottom_left = center_left + top_w + east_w + Inches(0.2)
        bottom_top = center_top
        slide.shapes.add_picture(imgs["Bottom View"],bottom_left,bottom_top,width=bottom_w, height=bottom_h)
        util.add_text_box(slide,bottom_left,bottom_top-Inches(0.2),Inches(0.2),Inches(0.2),"Bottom View",10)

        acf_w, acf_h = img_sizes["ACF Pad"]
        acf_left = center_left + top_w + east_w+ Inches(0.2)
        acf_top = center_top + bottom_h
        slide.shapes.add_picture(imgs["ACF Pad"],acf_left,acf_top,width=acf_w, height=acf_h)
        util.add_text_box(slide,acf_left,acf_top+acf_h,Inches(0.2),Inches(0.2),"ACF Pad",10)


        ng_w, ng_h = img_sizes["NG Location"]
        slide.shapes.add_picture(imgs["NG Location"],
                                 center_left + top_w + east_w +bottom_w + Inches(0.2),
                                 center_top,
                                 width=ng_w, height=ng_h)

    # Build slides
    for idx, imgs in enumerate(images_list.items()):
        if idx % max_per_slide == 0:
            slide = prs.slides.add_slide(prs.slide_layouts[6])
            util.add_text_box(slide,0,0,prs.slide_width,Inches(0.25),title,18,True)
        offset_x = (idx % max_per_slide) * Inches(prs_width/3)
        item = {}
        for i,iVal in enumerate(imgs[1]):
            for jdx,jVal in iVal.items():
                if  jdx == "North":
                    jVal = util.rotate_image(jVal,-180)
                if  jdx == "West":
                    jVal = util.rotate_image(jVal,-90)
                if  jdx == "East":
                    jVal = util.rotate_image(jVal,90)
                item[jdx] = jVal
        add_expanded_layout(slide, item, offset_x)


